﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class clsDMChiTietDonViTinh
    {
        private string mDienGiai = "";
        private double mHeSo = 0.0;
        private string mKhoa = "";
        private string mKhoaDonViTinh = "";
        private string mKhoaHangHoa = "";

        public bool Delete(string pKhoa)
        {
            IDataParameter parameter = null;
            bool flag=false;
            try
            {
                string commandText = " DELETE FROM DM_ChiTietDonViTinhHangHoa WHERE Khoa = '" + pKhoa + "'";
                bool flag2 = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }) > 0;
                flag = flag2;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public DataTable GetList(string pKhoaHangHoa = "")
        {
            IDataParameter parameter = null;
            DataTable table;
            try
            {
                string commandText = "SELECT CT.Khoa, CT.KhoaHangHoa, CT.KhoaDonViTinh, DVT.Ten" + modGeneral.H_LANGUAGE + " As Ten, CT.Heso, CT.DienGiai FROM DM_ChiTietDonViTinhHangHoa CT LEFT JOIN DM_DonViTinh DVT on CT.KhoaDonViTinh = DVT.Khoa  WHERE CT.KhoaHangHoa = '" + pKhoaHangHoa + "' ORDER BY CT.HeSo";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public bool isDupplicateCode(string pKhoa, string pKhoaHangHoa, string pKhoaDonViTinh)
        {
            IDataParameter parameter = null;
            bool flag=false;
            try
            {
                string commandText = " SELECT KhoaDonViTinh From DM_HangHoa Where Khoa = '" + pKhoaHangHoa.Trim() + "'";
                DataTable table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }).Tables[0];
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(table.Rows[0]["KhoaDonViTinh"], pKhoaDonViTinh, false))
                {
                    return true;
                }
                commandText = "SELECT * FROM DM_ChiTietDonViTinhHangHoa WHERE Rtrim(KhoaDonViTinh) = '" + pKhoaDonViTinh.Trim() + "' And Rtrim(KhoaHangHoa) = '" + pKhoaHangHoa.Trim() + "' AND Khoa <> '" + pKhoa + "'";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }).Tables[0];
                if (table.Rows.Count > 0)
                {
                    return true;
                }
                flag = false;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool Load(string pKhoa)
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[4];
            try
            {
                string commandText = "SELECT * FROM DM_ChiTietDonViTinhHangHoa WHERE Khoa = '" + pKhoa + "'";
                IDataReader dataReader = DBHelper.ExecuteReader(modGeneral.ConnectionString, CommandType.Text, commandText, commandParameters);
                if (dataReader.Read())
                {
                    this.mKhoa = Conversions.ToString(modGeneral.GetValue(dataReader, 0, modGeneral.typeOfColumn.GetString));
                    this.mKhoaHangHoa = Conversions.ToString(modGeneral.GetValue(dataReader, 1, modGeneral.typeOfColumn.GetString));
                    this.mKhoaDonViTinh = Conversions.ToString(modGeneral.GetValue(dataReader, 2, modGeneral.typeOfColumn.GetString));
                    this.mHeSo = Conversions.ToDouble(modGeneral.GetValue(dataReader, 3, modGeneral.typeOfColumn.GetDecimal));
                    this.mDienGiai = Conversions.ToString(modGeneral.GetValue(dataReader, 4, modGeneral.typeOfColumn.GetString));
                }
                dataReader.Close();
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool Save()
        {
            bool flag2;
            IDataParameter[] commandParameters = new IDataParameter[5];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, this.mKhoa);
                commandParameters[1] = DBHelper.createParameter("@KhoaHangHoa", DbType.Int64, ParameterDirection.Input, this.mKhoaHangHoa);
                commandParameters[2] = DBHelper.createParameter("@KhoaDonViTinh", DbType.Int64, ParameterDirection.Input, this.mKhoaDonViTinh);
                commandParameters[3] = DBHelper.createParameter("@Heso", DbType.Double, ParameterDirection.Input, this.mHeSo);
                commandParameters[4] = DBHelper.createParameter("@DienGiai", DbType.Int64, ParameterDirection.Input, this.mDienGiai);
                bool flag = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "sp_DM_ChiTietDonViTinh", commandParameters) > 0;
                flag2 = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag2 = false;
                ProjectData.ClearProjectError();
                return flag2;
                ProjectData.ClearProjectError();
            }
            return flag2;
        }

        public void SetBlank()
        {
            this.mKhoaHangHoa = "";
            this.mKhoaDonViTinh = "";
            this.mHeSo = 0.0;
        }

        public string DienGiai
        {
            get
            {
                return this.mDienGiai;
            }
            set
            {
                this.mDienGiai = value;
            }
        }

        public double HeSo
        {
            get
            {
                return this.mHeSo;
            }
            set
            {
                this.mHeSo = value;
            }
        }

        public string Khoa
        {
            get
            {
                return this.mKhoa;
            }
            set
            {
                this.mKhoa = value;
            }
        }

        public string KhoaDonViTinh
        {
            get
            {
                return this.mKhoaDonViTinh;
            }
            set
            {
                this.mKhoaDonViTinh = value;
            }
        }

        public string KhoaHangHoa
        {
            get
            {
                return this.mKhoaHangHoa;
            }
            set
            {
                this.mKhoaHangHoa = value;
            }
        }
    }
}

